#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

class Computer
{
public:
    Computer(const char * brand,double price)
    //char * _brand = new char[strlen(brand) + 1]();
    : _brand(new char[strlen(brand) + 1]())
    , _price(price)
    {
        strcpy(_brand,brand);
    }

    void print(){
        cout << "brand:" << _brand << endl;
        cout << "price:" << _price << endl;
    }

    ~Computer(){
        cout << "~Computer()" << endl;
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
    }
private:
    char * _brand;
    double _price;
};

void test0(){
    Computer pc("Apple",20000);
    pc.print();

    Computer pc2("Xiaomi",8000);
    pc2.print();
    //对象销毁时，一定会调用析构函数
    //析构函数执行完，对象并没有被销毁
    //
    //不要手动调用析构函数
    //让对象销毁时自动调用析构函数
    /* pc2.~Computer(); */
    /* pc2.print(); */

    cout << "over" << endl;
}

void test1(){
    /* int * p1 = new int(10); */
    /* char * p = new char[6]{'h','e','l','l','o'}; */
    int * p1 = nullptr;
    char * p = nullptr;

    cout << "start" << endl;
    cout << p1 << endl;
    //输出流运算符对char*有默认重载效果
    //会自动访问指针所指向的内容
    cout << p << endl;
    cout << "over" << endl;
}

int main(void){
    test1();
    return 0;
}
